package wec_wallet.ebo.com.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.squareup.picasso.Picasso;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import bassproject.ebo.com.ebobass.activity.BaseFragment;
import bassproject.ebo.com.ebobass.http_utils.interfaces.IHttpPostListener;
import bassproject.ebo.com.ebobass.http_utils.utils.Ipost;
import wec_wallet.ebo.com.utils.URLs;
import bassproject.ebo.com.ebobass.utils.IToast;
import bassproject.ebo.com.ebobass.utils.I_Share;
import bassproject.ebo.com.ebobass.utils.Lg;
import bassproject.ebo.com.ebobass.view.CircleImageView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import wec_wallet.ebo.com.R;
import wec_wallet.ebo.com.activity.main.MainActivity;
import wec_wallet.ebo.com.activity.options.ServiceActivity;
import wec_wallet.ebo.com.activity.wallet.TransferRecordsActivity;
import wec_wallet.ebo.com.adapter.EntryAdapter;
import wec_wallet.ebo.com.bean.EntryBean;
import wec_wallet.ebo.com.common.Constant;
import wec_wallet.ebo.com.utils.Functions;

public class FirstFragment extends BaseFragment {
    ArrayList<EntryBean> list = new ArrayList<>();
    @BindView(R.id.first_mine)
    CircleImageView firstMine;
    @BindView(R.id.first_right)
    ImageView firstRight;
    @BindView(R.id.first_total)
    TextView firstTotal;
    @BindView(R.id.first_service)
    ImageView firstService;
    @BindView(R.id.first_rv_entry)
    RecyclerView firstRvEntry;
    @BindView(R.id.first_advert)
    ImageView firstAdvert;
    Unbinder unbinder;
    public static FirstFragment newInstance() {
        FirstFragment f = new FirstFragment();
        Bundle args = new Bundle();
        f.setArguments(args);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_first, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView(view);
        return view;
    }

    private void initView(View view) {
        MainActivity activity=(MainActivity) getActivity();
        /**
         * 得到Activity的Handler
         */
        firstRvEntry.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        EntryAdapter entryAdapter = new EntryAdapter(getActivity());
        firstRvEntry.setAdapter(entryAdapter);
        entryAdapter.setOnItemClickLitener(new EntryAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                ((MainActivity)getActivity()).gotoPage(1);
            }
        });
        getData();
        String aPath = URLs.BASE_URL + "/" + I_Share.readAvatar();
        Lg.d(aPath);
        Picasso.get().load(aPath).into(firstMine);
        getNotice();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.first_mine, R.id.first_right, R.id.first_service})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.first_mine://我的
                ((MainActivity)getActivity()).gotoPage(3);
                break;
            case R.id.first_right://转账记录
                startActivity(new Intent(getActivity(), TransferRecordsActivity.class));
                break;
            case R.id.first_service://客服
                startActivity(new Intent(getActivity(), ServiceActivity.class));
                break;
        }
    }

    private void getData() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", I_Share.readAccountToken());
        map.put("uid", I_Share.readAccountUserid());

        Functions.post(getActivity(), Constant.URL_PROPERTY, map, new
                ListenerProperty(), false, true);
    }

    private void getNotice() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", I_Share.readAccountToken());
        map.put("uid", I_Share.readAccountUserid());

        Functions.post(getActivity(), URLs.URL_NOTICE, map, new IHttpPostListener() {
            @Override
            public void responseSuccess(String result) {
                try {
                    if (!TextUtils.isEmpty(result)) {
                        JSONObject resultObject = new JSONObject(result);
                        String status = resultObject.getString("error");
                        if (resultObject.has("is_auth") && resultObject.getString("is_auth").equals
                                ("0")) {
                            IToast.show(resultObject.getString("error_msg"));
                            Functions.relogin(getActivity());//登录过期
                            return;
                        }
                        if (resultObject.has("error_msg")) {
                            IToast.show(resultObject.getString("error_msg"));
                            return;
                        }
                        if (status.equals("0")) {
                            JSONObject dataJson = resultObject.getJSONObject("data");
                            Glide.with(getActivity()).load(dataJson.getString("url")).into(firstAdvert);
                        }
                    }
                }catch (Exception e){
                    Lg.d(e.toString());
                }
            }

            @Override
            public void onTimeOut() {

            }
        }, false, true);
    }

    private class ListenerProperty implements IHttpPostListener {
        @Override
        public void onTimeOut() {

        }
        @Override
        public void responseSuccess(String result) {
            if (Ipost.isTimeOut(result)) {
                return;
            }
            if (null != result) {
                try {

                    JSONObject resultObject = new JSONObject(result);
                    String status = resultObject.getString("error");
                    if (resultObject.has("is_auth") && resultObject.getString("is_auth").equals
                            ("0")) {
                        IToast.show(resultObject.getString("error_msg"));
                        Functions.relogin(getActivity());//登录过期
                        return;
                    }
                    if (resultObject.has("error_msg")) {
                        IToast.show(resultObject.getString("error_msg"));
                        return;
                    }
                    if (status.equals("0")) {
                        String count = resultObject.getString("user_count_cny");
                        firstTotal.setText(Functions.getOrdinaryCounting(count) + " CNY");
                    }

                } catch (Exception e) {
                    Lg.d(getClass() + ",接口解析异常：\n" + e.toString());
                }
            } else {
                Lg.d(" result=null");

            }
        }
    }
}
